package com.design_pattern._01_create._01_singleton;

/**
 * 错误演示，线程不安全
 */
public class BadTestDriver {
    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            createThread();
        } // 会发现可能有几个打印的对象不一样
    }

    private static void createThread() {
        Thread badThread = new Thread(() -> {
            Bad instance = Bad.getInstance();
            System.out.println(instance);
        });
        badThread.start();
    }
}

class Bad{
    private static Bad instance;
    private Bad() {
        super();
    }

    public static Bad getInstance(){
        if(instance==null){
            instance = new Bad();
        }
        return instance;
    }
}